package com.ludaxu.zhbj;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.ludaxu.zhbj.fragment.ContentFragment;
import com.ludaxu.zhbj.fragment.LeftMenuFragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;

/**
 * 主页面
 * 
 * @author ludaxu
 * 
 */
public class MainActivity extends SlidingFragmentActivity {
	// ctrl+shift+y把大写变成小写,ctrl+shift+x把小写变成大写
	private static final String FRAGMENT_Left_MENU = "fragment_left_menu";
	private static final String FRAGMENT_CONTENT = "fragment_content";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		// 设置左边的侧边栏
		setBehindContentView(R.layout.left_menu);
		// 得到侧边栏对象
		SlidingMenu slidingMenu = getSlidingMenu();
		// 设置侧边栏的触摸模式为全屏模式
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// 得到屏幕的宽度
		int width = getWindowManager().getDefaultDisplay().getWidth();

		// 设置拉开侧边栏后,预留屏幕的宽度
		slidingMenu.setBehindOffset(width * 200 / 320);
		initFragment();
	}

	/**
	 * 初始化fragment,将fragment数据填充给布局文件
	 */
	private void initFragment() {
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();// 开启事务
		// 用fragment替换侧边栏.,侧边栏和内容栏属于同一个activity
		transaction.replace(R.id.fl_left_menu, new LeftMenuFragment(),
				FRAGMENT_Left_MENU);
		// 用fragment替换主页面的内容栏
		transaction.replace(R.id.fl_content, new ContentFragment(),
				FRAGMENT_CONTENT);
		transaction.commit();// 提交事务
	}

	/**
	 * 获取侧边栏对象
	 * 
	 * @return
	 */
	public LeftMenuFragment getLeftMenuFragment() {
		FragmentManager fm = getSupportFragmentManager();
		LeftMenuFragment fragment = (LeftMenuFragment) fm
				.findFragmentByTag(FRAGMENT_Left_MENU);
		return fragment;
	}

	/**
	 * 获取主页面的fragment
	 * 
	 * @return
	 */
	public ContentFragment getContentFragment() {
		FragmentManager fm = getSupportFragmentManager();
		ContentFragment fragment = (ContentFragment) fm
				.findFragmentByTag(FRAGMENT_CONTENT);
		return fragment;
	}

}
